package com.bosssoft.ecds.controller;

import com.bosssoft.ecds.common.response.CommonCode;
import com.bosssoft.ecds.common.response.QueryResponseResult;
import com.bosssoft.ecds.common.response.ResponseResult;
import com.bosssoft.ecds.entity.dto.QueryDTO;
import com.bosssoft.ecds.entity.dto.ReceiveFinanceapplyDto;
import com.bosssoft.ecds.entity.dto.SentBillDto;
import com.bosssoft.ecds.entity.po.FbeStockFinanceapplyCheckPO;
import com.bosssoft.ecds.entity.vo.FbeStockFinanceapplyCheckVO;
import com.bosssoft.ecds.entity.vo.FbeStockFinanceapplyItemVO;
import com.bosssoft.ecds.service.impl.ConsumerService;
import com.bosssoft.ecds.service.impl.FbeStockFinanceapplyCheckServiceImpl;
import com.bosssoft.ecds.service.impl.FbeStockFinanceapplyItemServiceImpl;
import com.bosssoft.ecds.service.impl.FinanBillServiceImpl;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.math.BigInteger;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @author 张东海
 * @date 2020/8/19
 * @description 财政端申领信息记录Controller，提供了基本的增删改查
 *               同时附加了审核通过、审核退回以及获取指定ID的申领信息
 *               记录下的申请具体票据信息的接口
 */
@RestController
public class FbeStockFinanceapplyCheckController {

    @Resource
    FbeStockFinanceapplyCheckServiceImpl fbeStockFinanceapplyCheckService;

    @Resource
    FinanBillServiceImpl finanBillService;

    @Resource
    RabbitTemplate rabbitTemplate;

    @GetMapping("fbeStockFinanceapplyCheck")
    public QueryResponseResult getById(BigInteger id) {
        FbeStockFinanceapplyCheckPO financeapplyCheckPO = fbeStockFinanceapplyCheckService.getByPrimaryKey(id);
        return new QueryResponseResult(CommonCode.SUCCESS,financeapplyCheckPO);
    }

    @GetMapping("fbeStockFinanceapplyCheck/getAll")
    public QueryResponseResult getAllDetail(QueryDTO query) {
        List<FbeStockFinanceapplyCheckVO> voList = fbeStockFinanceapplyCheckService.getAllDetail(query);
        return new QueryResponseResult(CommonCode.SUCCESS,voList);
    }

    @PostMapping("fbeStockFinanceapplyCheck")
    public ResponseResult create(FbeStockFinanceapplyCheckPO fbeStockFinanceapplyCheck) {
        int insert = fbeStockFinanceapplyCheckService.insert(fbeStockFinanceapplyCheck);
        if(insert > 0){
            return ResponseResult.SUCCESS();
        }else {
            return ResponseResult.FAIL();
        }
    }

    @PutMapping("fbeStockFinanceapplyCheck")
    public ResponseResult update(FbeStockFinanceapplyCheckPO fbeStockFinanceapplyCheck) {
        fbeStockFinanceapplyCheckService.update(fbeStockFinanceapplyCheck);
        return ResponseResult.SUCCESS();
    }

    @DeleteMapping("fbeStockFinanceapplyCheck")
    public ResponseResult delete(BigInteger id) {
        fbeStockFinanceapplyCheckService.delete(id);
        return ResponseResult.SUCCESS();
    }

    @PutMapping("fbeStockFinanceapplyCheck/pass")
    public ResponseResult passApply(BigInteger id, String changeSitu){
        FbeStockFinanceapplyCheckPO financeapplyCheckPO = fbeStockFinanceapplyCheckService.getByPrimaryKey(id);
        financeapplyCheckPO.setFChangeDate(LocalDateTime.now());
        financeapplyCheckPO.setFStatus(2);
        financeapplyCheckPO.setFChangeSitu(changeSitu);
        fbeStockFinanceapplyCheckService.update(financeapplyCheckPO);
        fbeStockFinanceapplyCheckService.submit(financeapplyCheckPO);
        List<FbeStockFinanceapplyItemVO> items = fbeStockFinanceapplyCheckService.getItems(financeapplyCheckPO.getfId());
        ArrayList<ReceiveFinanceapplyDto> receiveFinanceapplyDtos = new ArrayList<>();
        for (FbeStockFinanceapplyItemVO item : items) {
            ReceiveFinanceapplyDto receiveFinanceapplyDto = new ReceiveFinanceapplyDto();
            receiveFinanceapplyDto.setAuthor(financeapplyCheckPO.getfAuthor());
            receiveFinanceapplyDto.setNumber(item.getfNumber());
            receiveFinanceapplyDto.setBillPrecode(item.getfBillPrecode());
            receiveFinanceapplyDtos.add(receiveFinanceapplyDto);
        }
        List<SentBillDto> sentBillDtos = finanBillService.outBills(receiveFinanceapplyDtos);
        rabbitTemplate.convertAndSend("amq.direct","billKey",sentBillDtos);
        return ResponseResult.SUCCESS();
    }

    @PutMapping("fbeStockFinanceapplyCheck/return")
    public ResponseResult returnApply(BigInteger id, String changeSitu){
        FbeStockFinanceapplyCheckPO financeapplyCheckPO = fbeStockFinanceapplyCheckService.getByPrimaryKey(id);
        financeapplyCheckPO.setFChangeDate(LocalDateTime.now());
        financeapplyCheckPO.setFStatus(3);
        financeapplyCheckPO.setFChangeSitu(changeSitu);
        fbeStockFinanceapplyCheckService.update(financeapplyCheckPO);
        fbeStockFinanceapplyCheckService.submit(financeapplyCheckPO);
        return ResponseResult.SUCCESS();
    }

    @GetMapping("fbeStockFinanceapplyCheck/items")
    public QueryResponseResult getItems(BigInteger id) {
        List<FbeStockFinanceapplyItemVO> items = fbeStockFinanceapplyCheckService.getItems(id);
        return new QueryResponseResult(CommonCode.SUCCESS,items);
    }
}
